package com.wanczy.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanczy.pojo.Cart;
import com.wanczy.pojo.EvalUserGoods;
import com.wanczy.pojo.Favorite;
import com.wanczy.pojo.Goods;
import com.wanczy.pojo.Shop;
import com.wanczy.service.DetailService;

@Controller
public class DetailController {
	
	@Autowired
	private DetailService detailService;
	
	@RequestMapping(value="/detail")
	public String detailQuery(@RequestParam("goodsId") Integer goodsId,Map<String,Object> map) {
		Goods goods = detailService.findByGoodsId(goodsId);
		map.put("goods", goods);
		return "/goods/detail";
	}
	
	@ResponseBody
	@RequestMapping(value="/detailQueryShop")
	public List<Shop> detailQueryShop(@RequestParam("shopId") Integer shopId) {
		Shop shop = detailService.findByShopId(shopId);
		List<Shop> list = new ArrayList<Shop>();
		list.add(shop);
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/detailQueryEval")
	public List<EvalUserGoods> detailQueryEval(@RequestParam("goodsId") Integer goodsId) {
		List<EvalUserGoods> list = detailService.queryEval(goodsId);
		for (EvalUserGoods evalUserGoods : list) {
			Date date =new java.sql.Date( evalUserGoods.getEvalTime().getTime());
			evalUserGoods.setEvalTime(date);
		}
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/addFavorite")
	public int addFavorite(Favorite favorite) {
		int count = 0;
		Favorite fav = detailService.queryFavorite(favorite);
		if(fav == null) {
			count = detailService.addFavorite(favorite);
		}
		return count;
	}
	
	@ResponseBody
	@RequestMapping(value="/addCart")
	public int addCart(Cart cart) {
		int count = 0;
		Cart c = detailService.queryCart(cart);
		if(c == null) {
			count = detailService.addCart(cart);
		}
		return count;
	}
}
